﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMP_QuanLySV.BLL;
using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.Presentation
{
    public partial class UC_TimKiemSV : UserControl
    {
        Form parent;
        BLLSinhVien BLL = new BLLSinhVien();
        BLLDanToc DT = new BLLDanToc();
        BLLTonGiao TG = new BLLTonGiao();
        BLLLop_SV LOP = new BLLLop_SV();
        List<Lop_SV> listLop;
        List<DanToc> listDT;
        List<TonGiao> listTG;

        string trangThai = "";
        SinhVien selectedInstance;
        int selectedIndex;

        // constructor
        public UC_TimKiemSV(Form parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        private void UC_TimKiemSV_Load(object sender, EventArgs e)
        {
            // nap du lieu cho datagridview
            dataGridViewX1.DataSource = BLL.returnListSinhVien();
            dataGridViewX1.Columns[19].Visible = false;
            dataGridViewX1.Columns[20].Visible = false;
            dataGridViewX1.Columns[21].Visible = false;
            dataGridViewX1.Columns[22].Visible = false;
            dataGridViewX1.Columns[23].Visible = false;
            dataGridViewX1.Columns[24].Visible = false;
            dataGridViewX1.Columns[25].Visible = false;
            dataGridViewX1.Columns[26].Visible = false;
            dataGridViewX1.Columns[27].Visible = false;
            dataGridViewX1.Columns[28].Visible = false;

            // nap du lieu cho combobox
            listLop = LOP.returnListLop();
            cbxMaLop.DataSource = listLop;
            cbxMaLop.DisplayMember = "TenLop";
            cbxMaLop.ValueMember = "LopID";
            cbxMaLop.SelectedIndex = 0;

            listDT = DT.returnListDanToc();
            cbxDanToc.DataSource = listDT;
            cbxDanToc.DisplayMember = "TenDanToc";
            cbxDanToc.ValueMember = "DanTocID";
            cbxDanToc.SelectedIndex = 0;

            listTG = TG.returnListTonGiao();
            cbxTonGiao.DataSource = listTG;
            cbxTonGiao.DisplayMember = "TenTonGiao";
            cbxTonGiao.ValueMember = "TonGiaoID";
            cbxTonGiao.SelectedIndex = 0;

            selectedIndex = 0;
            selectedInstance = BLL.retrieveByIndex(0);
            updateTextBoxes(selectedInstance);

            groupBox1.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (selectedInstance.SVID == "")
            {
                MessageBox.Show("Bạn phải chọn sinh viên nào cần xóa", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa sinh viên này không?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.Yes)
                {
                    // xoa doi tuong va update lai view
                    
                    BLL.deleteSinhVien(selectedInstance);

                    dataGridViewX1.DataSource = BLL.returnListSinhVien();
                  
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            selectedInstance.HoLot = txtHoLot.Text;
            selectedInstance.Ten = txtTen.Text;
            selectedInstance.NgaySinh = txtNgaySinh.Text;
            selectedInstance.NoiSinh = txtNoiSinh.Text;
            selectedInstance.DiaChi = txtDiaChi.Text;
            selectedInstance.DienThoai = txtDienThoai.Text;
            selectedInstance.Email = txtEmail.Text;
            selectedInstance.NamTNPT = txtNamTNPT.Text;
            selectedInstance.QuocTich = txtQuocTich.Text;
            selectedInstance.HoTenCha = txtHoTenCha.Text;
            selectedInstance.HoTenMe = txtHoTenMe.Text;
            selectedInstance.NgheNghiepCha = txtNgheNghiepCha.Text;
            selectedInstance.NgheNghiepMe = txtNgheNghiepMe.Text;
            selectedInstance.DanTocID = cbxDanToc.SelectedValue.ToString();
            selectedInstance.LopID = cbxMaLop.SelectedValue.ToString();
            selectedInstance.TonGiaoID = cbxTonGiao.SelectedValue.ToString();
            selectedInstance.GhiChu = txtGhiChu.Text;
            if (cbxGioiTinh.SelectedIndex == 0) selectedInstance.GioiTinh = "Nam";
            else selectedInstance.GioiTinh = "Nữ";

            if (BLL.kiemTra(selectedInstance))
            {
                // update csdl va view
                if (trangThai == "dang_sua")
                {
                    BLL.updateSinhVien(selectedInstance);
                    selectedIndex = 0;
                    selectedInstance = BLL.retrieveByIndex(selectedIndex);
                    updateTextBoxes(selectedInstance);
                    groupBox1.Enabled = false;
                    btnXoa.Enabled = true;
                    btnSua.Enabled = true;
                    btnLuu.Enabled = false;
                    btnHuy.Enabled = false;
                    dataGridViewX1.Enabled = true;
                }
                dataGridViewX1.DataSource = BLL.returnListSinhVien();
            }
            else
            {
                MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            dataGridViewX1.DataSource = BLL.returnListSinhVien();
            groupBox1.Enabled = false;
            btnXoa.Enabled = true;
            btnSua.Enabled = true;
            updateTextBoxes(selectedInstance);
            groupBox1.Enabled = false;
            btnXoa.Enabled = true;
            btnSua.Enabled = true;
            btnLuu.Enabled = false;
            btnHuy.Enabled = false;
            dataGridViewX1.Enabled = true;
        }

        private void btnTimkiemSV_Click(object sender, EventArgs e)
        {
            if (txtMSSVFind.Text == "" && txtTenFind.Text == "")
            {
                dataGridViewX1.DataSource = BLL.returnListSinhVien();
            }
            else
            {
                dataGridViewX1.DataSource = BLL.BindingSV(txtMSSVFind.Text, txtTenFind.Text);
                dataGridViewX1.Columns[19].Visible = false;
                dataGridViewX1.Columns[20].Visible = false;
                dataGridViewX1.Columns[21].Visible = false;
                dataGridViewX1.Columns[22].Visible = false;
                dataGridViewX1.Columns[23].Visible = false;
                dataGridViewX1.Columns[24].Visible = false;
                dataGridViewX1.Columns[25].Visible = false;
                dataGridViewX1.Columns[26].Visible = false;
                dataGridViewX1.Columns[27].Visible = false;
                dataGridViewX1.Columns[28].Visible = false;
            }
        }

        void updateTextBoxes(SinhVien sv)
        {
            txtHoLot.Text = sv.HoLot;
            txtTen.Text = sv.Ten;
            for (int i = 0; i < listLop.Count; i++)
                if (listLop[i].LopID == sv.LopID)
                {
                    cbxMaLop.SelectedIndex = i;
                    break;
                }
            for (int i = 0; i < listTG.Count; i++)
                if (listTG[i].TonGiaoID == sv.TonGiaoID)
                {
                    cbxTonGiao.SelectedIndex = i;
                    break;
                }
            for (int i = 0; i < listDT.Count; i++)
                if (listDT[i].DanTocID == sv.DanTocID)
                {
                    cbxDanToc.SelectedIndex = i;
                    break;
                }

            txtNgaySinh.Text = sv.NgaySinh;
            txtNamTNPT.Text = sv.NamTNPT;
            txtNoiSinh.Text = sv.NoiSinh;
            if (sv.GioiTinh == "Nam")
                cbxGioiTinh.SelectedIndex = 0;
            else cbxGioiTinh.SelectedIndex = 1;

            txtDiaChi.Text = sv.DiaChi;
            txtDienThoai.Text = sv.DienThoai;
            txtEmail.Text = sv.Email;
            txtQuocTich.Text = sv.QuocTich;
            txtHoTenCha.Text = sv.HoTenCha;
            txtHoTenMe.Text = sv.HoTenMe;
            txtNgheNghiepCha.Text = sv.NgheNghiepCha;
            txtNgheNghiepMe.Text = sv.NgheNghiepMe;
            txtGhiChu.Text = sv.GhiChu;
        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedIndex = dataGridViewX1.SelectedCells[0].RowIndex;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            updateTextBoxes(selectedInstance);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            txtMSSVFind.Enabled = false;
            txtTenFind.Enabled = false;
            btnTimkiemSV.Enabled = false;
            btnXoa.Enabled = false;
            btnSua.Enabled = false;

            btnLuu.Enabled = true;
            btnHuy.Enabled = true;

            groupBox1.Enabled = true;
            dataGridViewX1.Enabled = false;

            trangThai = "dang_sua";
        }
    }
}
